﻿using System;
using System.Collections.Generic;
using System.Text;
using CPFrameWork.Utility.DbOper;
using CPFrameWork.Global;
using System.Data;
using CPFrameWork.Utility;
using CPFrameWork.UIInterface.Grid;

namespace BusinessManage
{
    public class HatchRoom
    {

        /// <summary>
        /// 获取下拉框数据源
        /// author：WANGYY
        /// </summary>
        /// <param name="gridObj"></param>
        /// <returns></returns>
        public Dictionary<string, string> ReadColumnListData(FieldSource sourceObj)
        {
            Dictionary<string, string> col = new Dictionary<string, string>();
            DbHelper dbHelper = new DbHelper(sourceObj.FieldEnumDataIns, CPAppContext.CurDbType());
            string strSql = sourceObj.FieldEnumDataSource;
            try
            {

                strSql = CPExpressionHelper.Instance.RunCompile(strSql);
                DataTable dt = dbHelper.ExecuteDataTable(strSql);
                DataTable dtNew = new DataTable();
                dtNew.Columns.Add(new DataColumn() { ColumnName = "textEx", DataType = typeof(string) });


                dtNew.Columns.Add(new DataColumn() { ColumnName = "valueEx", DataType = typeof(string) });
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow newDR = dtNew.NewRow();

                    #region 支持配置规范“ 字段  as value, 文本  as text”
                    DataTable tb = dr.Table;
                    foreach (DataColumn column in tb.Columns)
                    {
                        if (column.ColumnName.ToLower() == "value")
                        {
                            if (column.DataType == Type.GetType("System.Boolean"))
                            {
                                newDR["valueEx"] = dr[column].ToString().Trim().ToLower();
                            }
                            else
                            {
                                newDR["valueEx"] = dr[column];
                            }
                            continue;
                        }
                        else if (column.ColumnName.ToLower() == "text")
                        {
                            newDR["textEx"] = dr[column];
                            continue;
                        }
                        //为了兼容原来版本 “第一个为text，第二个为value”

                        newDR["textEx"] = dr[0];
                        if (dt.Columns[1].DataType == Type.GetType("System.Boolean"))
                        {
                            newDR["valueEx"] = dr[1].ToString().Trim().ToLower();
                        }
                        else
                        {
                            newDR["valueEx"] = dr[1];
                        }
                        break;


                    }
                    #endregion

                    dtNew.Rows.Add(newDR);
                }
                col.Add("Column", CPUtils.DataTable2Json(dtNew));
            }
            catch (Exception ex)
            {
                throw new Exception("执行sql语句:" + strSql + "出错，详细信息为：" + ex.Message);
            }


            return col;
        }


        public CPWebApiBaseReturnEntity SaveYW_GivetohallInfo(Items item, int CurUserId,string GridCode)
        {
            CPWebApiBaseReturnEntity re = new CPWebApiBaseReturnEntity();
            try
            {
               
                CPGrid grid = CPGridEngine.Instance(CurUserId).GetGrid(GridCode, false, false);
                DbHelper dbHelper = new DbHelper(grid.DbIns, CPAppContext.CurDbType());

                #region 列表数据源结果集
                StringBuilder stringBuilder = new StringBuilder();
                string sql = "select VarietyId,VarietyName,GoodsId,GoodsName,OrderChildId from dbo.YW_OrderDetail  where id=" + item.Id;
                stringBuilder.Append(sql);              
                DataTable dt=dbHelper.ExecuteDataSet(stringBuilder.ToString()).Tables[0];
                #endregion

                StringBuilder sb = new StringBuilder();
                string[] pkArray = grid.PKFieldName.Split(',');
                string strSql = "";
                strSql = "INSERT INTO   YW_GivetohallInfo";
                strSql += "(OrderId,OrderChildId,BaseId,BaseName,HallId,HallName,VarietyId,VarietyName,GoodsId,GoodsName,BuyChickCount,HallLossCount,CreateUserId,CreateTime,ModifyTime,Remarks)";
                strSql += "VALUES ";
                strSql += " ('" + item.OrderId.Trim() + "','"+dt.Rows[0]["OrderChildId"].ToString() + "','"+item.BaseId+"','"+item.BaseName+"','"+item.HallId+"','"+item.HallName+"',";
                strSql += "'" + dt.Rows[0][0].ToString() + "','" + dt.Rows[0][1].ToString() + "','" + dt.Rows[0][2].ToString() + "','" + dt.Rows[0][3].ToString() + "'," + item.BuyChickCount + ",";
                strSql += item.HallLossCount + ",'" + CurUserId + "','" + DateTime.Now + "','" + DateTime.Now + "','" + item.Remarks + "')";
                sb.Append(strSql);


                if (sb.Length > 0)
                {
                    dbHelper.ExecuteNonQuery(sb.ToString());
                }
                re.Result = true;
                return re;
            }
            catch (Exception ex)
            {
                re.Result = false;
                re.ErrorMsg = ex.Message.ToString();
                return re;
            }
        }

        /// <summary>
        /// 复制分厅数据
        /// </summary>
        /// <param name="item"></param>
        /// <param name="CurUserId"></param>
        /// <param name="GridCode"></param>
        /// <returns></returns>
        public CPWebApiBaseReturnEntity CopyYW_GivetohallInfo(Items item, int CurUserId, string GridCode)
        {
            CPWebApiBaseReturnEntity re = new CPWebApiBaseReturnEntity();
            try
            {

                CPGrid grid = CPGridEngine.Instance(CurUserId).GetGrid(GridCode, false, false);
                DbHelper dbHelper = new DbHelper(grid.DbIns, CPAppContext.CurDbType());

                #region 列表数据源结果集
                StringBuilder stringBuilder = new StringBuilder();
                string sql = "select VarietyId,VarietyName,GoodsId,GoodsName,OrderChildId,BaseId,BaseName  from dbo.YW_GivetohallInfo  where id=" + item.Id;
                stringBuilder.Append(sql);
                DataTable dt = dbHelper.ExecuteDataSet(stringBuilder.ToString()).Tables[0];
                #endregion

                StringBuilder sb = new StringBuilder();
                string[] pkArray = grid.PKFieldName.Split(',');
                string strSql = "";
                strSql = "INSERT INTO   YW_GivetohallInfo";
                strSql += "(OrderId,OrderChildId,BaseId,BaseName,HallId,HallName,VarietyId,VarietyName,GoodsId,GoodsName,BuyChickCount,HallLossCount,CreateUserId,CreateTime,ModifyTime)";
                strSql += "VALUES ";
                strSql += " ('" + item.OrderId.Trim() + "','" + dt.Rows[0]["OrderChildId"].ToString() + "','" + dt.Rows[0]["BaseId"].ToString() + "','" + dt.Rows[0]["BaseName"].ToString() + "','" + item.HallId + "','" + item.HallName + "',";
                strSql += "'" + dt.Rows[0][0].ToString() + "','" + dt.Rows[0][1].ToString() + "','" + dt.Rows[0][2].ToString() + "','" + dt.Rows[0][3].ToString() + "'," + item.BuyChickCount + "," + item.HallLossCount + ",'" + CurUserId + "','" + DateTime.Now + "','" + DateTime.Now + "')";
                sb.Append(strSql);


                if (sb.Length > 0)
                {
                    dbHelper.ExecuteNonQuery(sb.ToString());
                }
                re.Result = true;
                return re;
            }
            catch (Exception ex)
            {
                re.Result = false;
                re.ErrorMsg = ex.Message.ToString();
                return re;
            }
        }
    }
}
